//
// Created by qsy on 2023/8/21.
//
#include <stdio.h>
#include "NodeSystemInfo.h"
#include "at24256.h"
#include "usart.h"
#include "USR-K2.h"

NodeSysInfoFrm NodeSysInfo;
SystemStatusFrm SystemStatus;
PCtoDeviceDataFrm  PCtoDeviceData;

void NodeInfo_Init(void)
{
    NodeInfo_Read_fromEEPROM();

    SystemStatus.Idle = 1;
    SystemStatus.Trial_Stop = 0;
    SystemStatus.Trial_On = 0;
    SystemStatus.Trial_Ready = 0;
    SystemStatus.DebugMode = 0;
    SystemStatus.Trial_Err = 0;
    SystemStatus.ZeroClear = 0;
    SystemStatus.Trial_Idle = 0;
}

//在eeprom内写入系统信息
void NodeInfo_Write_toEEPROM(void)
{
    //相当于首位检测位
    NodeSysInfo.ConfigStatus = 0xAA;  // 0b 1010 1010,表示已有EEPROM数据写入

    //以下数据由报文解释给提供,以下仅测试用
//    NodeSysInfo.IP_addr[0] = 192;
//    NodeSysInfo.IP_addr[1] = 168;
//    NodeSysInfo.IP_addr[2] = 0;
//    NodeSysInfo.IP_addr[3] = 100;
//    NodeSysInfo.SlaveID = 10;       //从机地址
//    NodeSysInfo.portNumber = 8080;   //端口号

    NodeSysInfo.reserve = 0;         //为了对齐八位

    NodeSysInfo.chksum = NodeSysInfo.IP_addr[0]+NodeSysInfo.IP_addr[1]+NodeSysInfo.IP_addr[2]+
                         NodeSysInfo.IP_addr[3]+NodeSysInfo.SlaveID + NodeSysInfo.reserve + NodeSysInfo.portNumber;

    at24cxx_write(EEPROM_START_ADDR, (uint8_t*)(&NodeSysInfo), sizeof(NodeSysInfo));

}

//读取eeprom内的信息
void NodeInfo_Read_fromEEPROM(void)
{
    uint8_t temp_chksum;

    at24cxx_read(EEPROM_START_ADDR,(uint8_t*)(&NodeSysInfo), sizeof(NodeSysInfo));

    // 读取EEPROM的ConfigStatus检查是否为 0xAA，如果是，表示eeprom被写入过
    if( NodeSysInfo.ConfigStatus == 0xAA)
    {
        // 需要对数据进行校验，确保从EEPROM读取数据是可靠的
        temp_chksum = NodeSysInfo.IP_addr[0]+NodeSysInfo.IP_addr[1]+NodeSysInfo.IP_addr[2]+
                      NodeSysInfo.IP_addr[3]+NodeSysInfo.SlaveID + NodeSysInfo.portNumber;
        if( NodeSysInfo.chksum == temp_chksum )
        {
            //读取出来，且校验成功

        }
        else  // 如果EEPROM读取数据 校验有错， 如下处理
        {
            NodeSysInfo.IP_addr[0] = 192;
            NodeSysInfo.IP_addr[0] = 168;
            NodeSysInfo.IP_addr[0] = 0;
            NodeSysInfo.IP_addr[0] = 100;

            NodeSysInfo.SlaveID = 10;
            NodeSysInfo.portNumber = 8080;
        }

    }
    else  // 如果EEPROM读取数据ConfigStatus 不为 0xAA，表示eeprom没有被写入过
    {
        NodeSysInfo.IP_addr[0] = 192;
        NodeSysInfo.IP_addr[0] = 168;
        NodeSysInfo.IP_addr[0] = 0;
        NodeSysInfo.IP_addr[0] = 100;

        NodeSysInfo.SlaveID = 10;
        NodeSysInfo.portNumber = 8080;

    }
}

//测试
//FE FA 0A C0 A8 00 64 1F 91
//192.168.0.100  8081

// USART1 接收数据后，报文解析
//uint8_t rx_buf_chk=0, i;
void USART1_RxData_Parse(void)
{
    if( USARTx_RX_BUF_MSG.NewFrmFlag == 1 && USARTx_RX_BUF_MSG.Buf[0]== 0xFE && USARTx_RX_BUF_MSG.Buf[1]== 0xFA)
    {
//        for(i=0;i<7;i++)
//            rx_buf_chk = rx_buf_chk + USARTx_RX_BUF_MSG.Buf[i];
//
//        if( rx_buf_chk ==  USARTx_RX_BUF_MSG.Buf[7])
//        {
        //ID
        NodeSysInfo.SlaveID     = USARTx_RX_BUF_MSG.Buf[2];
        //IP
        NodeSysInfo.IP_addr[0]  = USARTx_RX_BUF_MSG.Buf[3];
        NodeSysInfo.IP_addr[1]  = USARTx_RX_BUF_MSG.Buf[4];
        NodeSysInfo.IP_addr[2]  = USARTx_RX_BUF_MSG.Buf[5];
        NodeSysInfo.IP_addr[3]  = USARTx_RX_BUF_MSG.Buf[6];
        //端口号
        NodeSysInfo.portNumber  = (USARTx_RX_BUF_MSG.Buf[7] << 8) | USARTx_RX_BUF_MSG.Buf[8];
        //写入
        NodeInfo_Write_toEEPROM();
//        }
        usr_init();                 //涉及到了重启网口、读eeprom，初始开启时会有3s的延时
        USARTx_RX_BUF_MSG.NewFrmFlag = 0;  // 清除标志位
    }
}

// 调试上位机下发的报文协议
// FE  FA
//

//测试是否写入成功--成功
void SystemWrite_test(void)
{
    NodeSysInfoFrm NodeSysInfo2;
    NodeInfo_Write_toEEPROM();
    at24cxx_read(EEPROM_START_ADDR,(uint8_t*)(&NodeSysInfo2), sizeof(NodeSysInfo2));
    printf("data:%d\n",NodeSysInfo2.IP_addr[0]);
    printf("data:%d\n",NodeSysInfo2.IP_addr[1]);
    printf("data:%d\n",NodeSysInfo2.IP_addr[2]);
    printf("data:%d\n",NodeSysInfo2.IP_addr[3]);
    printf("data:%d\n",NodeSysInfo2.portNumber);
}





